﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.SQLite;

namespace KeyboardImageViewer
{
    /// <summary>
    /// Interaction logic for EditViewList.xaml
    /// </summary>
    public partial class EditViewList : Window
    {
        private int listId;
        private int listIndex;
        private List<string> orders;
        private TagAutoCompleteTextBox searchBox;

        public EditViewList(int listId, int listIndex)
        {
            InitializeComponent();

            this.orders = new List<string>();
            this.orders.Add("random");
            this.orders.Add("name");
            this.orders.Add("lastWriteTime");

            this.listId = listId;
            this.listIndex = listIndex;

            this.orderByBox.SelectionChanged += new SelectionChangedEventHandler(orderByBox_SelectionChanged);

            Dictionary<string, int> tagsSummary = Database.GetTagsSummary(ImageManager.Connection);
            this.searchBox = new TagAutoCompleteTextBox
            {
                Margin = new Thickness(70, 112, 170, 0),
                VerticalAlignment = VerticalAlignment.Top,
                TagsSummary = tagsSummary,
                IgnoreDashPrefix = true
            };

            this.mainGrid.Children.Add(this.searchBox);

            if (listId >= 0)
            {
                SQLiteCommand command = new SQLiteCommand("SELECT name, orderBy, ascending, rating, searchString, searchTags, searchFileName, untagged, customClause FROM viewLists WHERE id = " + this.listId, ImageManager.Connection);
                using (SQLiteDataReader reader = command.ExecuteReader())
                {
                    reader.Read();

                    this.nameBox.Text = reader.GetString("name");
                    this.orderByBox.SelectedIndex = orders.IndexOf(reader.GetString("orderBy"));

                    if (reader.GetBoolean("ascending"))
                    {
                        this.ascendingBox.SelectedIndex = 0;
                    }
                    else
                    {
                        this.ascendingBox.SelectedIndex = 1;
                    }

                    int ratingIndex = reader.GetInt32("rating");
                    if (ratingIndex < 0)
                    {
                        ratingIndex = 6;
                    }
                    this.ratingBox.SelectedIndex = ratingIndex;

                    //this.searchBox.Text = reader.GetString("searchString");
                    this.tagsSearchBox.IsChecked = reader.GetBoolean("searchTags");
                    this.fileNameSearchBox.IsChecked = reader.GetBoolean("searchFileName");
                    this.untaggedBox.IsChecked = reader.GetBoolean("untagged");
                    this.customClauseBox.Text = reader.GetString("customClause");

                    this.searchBox.AutoCompleteEnabled = reader.GetBoolean("searchTags");
                    this.searchBox.Text = reader.GetString("searchString");
                }
            }
        }

        private void orderByBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.ascendingBox.IsEnabled = this.orderByBox.SelectedIndex != this.orders.IndexOf("random");
        }

        private void okButton_Click(object sender, RoutedEventArgs e)
        {
            SQLiteConnection connection = ImageManager.Connection;

            if (this.nameBox.Text == "")
            {
                MessageBox.Show("Must specify a name.");
                return;
            }

            string name = this.nameBox.Text;
            string orderBy = this.orders[this.orderByBox.SelectedIndex];
            bool ascending = this.ascendingBox.SelectedIndex == 0;
            int rating = this.ratingBox.SelectedIndex;
            if (rating > 5)
            {
                rating = -1;
            }

            string searchString = this.searchBox.Text;
            bool searchTags = (bool)this.tagsSearchBox.IsChecked;
            bool searchFileName = (bool)this.fileNameSearchBox.IsChecked;
            bool untagged = (bool)this.untaggedBox.IsChecked;
            string customClause = this.customClauseBox.Text;

            SQLiteCommand command = new SQLiteCommand(connection);

            var nameParameter = new SQLiteParameter();
            command.Parameters.Add(nameParameter);
            nameParameter.Value = name;

            var ascendingParameter = new SQLiteParameter();
            command.Parameters.Add(ascendingParameter);
            ascendingParameter.Value = ascending;

            var searchTagsParameter = new SQLiteParameter();
            command.Parameters.Add(searchTagsParameter);
            searchTagsParameter.Value = searchTags;

            var searchFileNameParameter = new SQLiteParameter();
            command.Parameters.Add(searchFileNameParameter);
            searchFileNameParameter.Value = searchFileName;

            var untaggedParameter = new SQLiteParameter();
            command.Parameters.Add(untaggedParameter);
            untaggedParameter.Value = untagged;

            var customClauseParameter = new SQLiteParameter();
            command.Parameters.Add(customClauseParameter);
            customClauseParameter.Value = customClause;

            if (listId < 0)
            {
                command.CommandText = "INSERT INTO viewLists (name, listIndex, orderBy, ascending, rating, searchString, searchTags, searchFileName, untagged, customClause) VALUES " +
                    "(?, " + this.listIndex + ", '" + orderBy + "', ?, " + rating + ", '" + searchString + "', ?, ?, ?, ?)";
            }
            else
            {
                command.CommandText = "UPDATE viewLists SET name = ?, orderBy = '" + orderBy + "', ascending = ?, rating = " + rating + ", searchString = '" + searchString + "', searchTags = ?, searchFileName = ?, untagged = ?, customClause = ? WHERE id = " + this.listId;
            }

            command.ExecuteNonQuery();

            this.DialogResult = true;
        }

        private void tagsSearchBox_Click(object sender, RoutedEventArgs e)
        {
            if (this.searchBox != null)
            {
                this.searchBox.AutoCompleteEnabled = (bool)this.tagsSearchBox.IsChecked;
            }
        }
    }
}
